package com.feisuanyz.controller;

import com.feisuanyz.constant.ResultCodeConstant;
import com.feisuanyz.pojo.dto.CreateGroup;
import com.feisuanyz.pojo.dto.RestResult;
import com.feisuanyz.pojo.dto.UserDTO;
import com.feisuanyz.pojo.dto.UserLoginDTO;
import com.feisuanyz.pojo.query.QueryGroup;
import com.feisuanyz.pojo.query.UserQuery;
import com.feisuanyz.pojo.vo.UserVO;
import com.feisuanyz.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.lang.Boolean;
import java.lang.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author Chenke Zhao
 * @date 2025-03-06 10:18:05
 */
@Tag(name = "用户管理")
@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册: 校验入参的合法性，检查用户名和邮箱是否已存在，生成密码哈希，插入用户信息到users表
     *
     * @param userDTO 用户注册信息
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @Operation(summary = "用户注册: 校验入参的合法性，检查用户名和邮箱是否已存在，生成密码哈希，插入用户信息到users表")
    @ResponseBody
    public RestResult<Boolean> registerUser(@RequestBody @Validated(CreateGroup.class) UserDTO userDTO) {
        Boolean result = userService.registerUser(userDTO);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }

    /**
     * 用户登录: 校验入参的合法性，检查用户名是否存在，校验密码哈希是否匹配，生成JWT token
     *
     * @param userLoginDTO 用户登录信息
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @Operation(summary = "用户登录: 校验入参的合法性，检查用户名是否存在，校验密码哈希是否匹配，生成JWT token")
    @ResponseBody
    public RestResult<String> loginUser(@RequestBody @Validated UserLoginDTO userLoginDTO) {
        String result = userService.loginUser(userLoginDTO);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }

    /**
     * 身份验证: 校验入参的合法性，验证JWT token的有效性，查询用户信息
     *
     * @param userQuery 用户查询信息
     * @return
     */
    @RequestMapping(value = "/validate", method = RequestMethod.GET)
    @Operation(summary = "身份验证: 校验入参的合法性，验证JWT token的有效性，查询用户信息")
    public RestResult<UserVO> validateUser(@Validated(QueryGroup.class) UserQuery userQuery) {
        UserVO result = userService.validateUser(userQuery);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }
}
